package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbDictValue;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.List;

@Repository
public interface CmdbDictValueRepository extends JpaRepository<CmdbDictValue, Integer>, JpaSpecificationExecutor<CmdbDictValue> {
    List<CmdbDictValue> findAllByDictVIdIn(List<Long> id);

    @Query(value = "select label from cmdb_dict_value where dict_id = :dictId and value = :value", nativeQuery = true)
    String findDictValueByDictValueName(Integer dictId, String value);

    @Query(value = "select * from cmdb_dict_value where dict_id = :dictId and value in :values order by value+0 asc ", nativeQuery = true)
    List<CmdbDictValue> getAllCtrlValues(Integer dictId, List<String> values);

    @Query(value = "select * from cmdb_dict_value where dict_id = :dictId and value = :value", nativeQuery = true)
    CmdbDictValue findByValueAndDictId(Integer dictId, String value);

    @Query(value = "SELECT branch_no,subentry,dict_prompt FROM sysdictionary a WHERE a.DICT_ENTRY=1052", nativeQuery = true)
    List<Object[]> findRoomCodes();

    CmdbDictValue findByValueAndDictCode(String value, String dictCode);

    @Query(value = "select * from cmdb_dict_value order by dict_code,seq", nativeQuery = true)
    List<CmdbDictValue> findDictValueList();

    @Query(value = "select * from cmdb_dict_value where (label like CONCAT('%',:labelOrValue,'%') or value like CONCAT('%',:labelOrValue,'%')) and dict_code = :dictCode", nativeQuery = true)
    List<CmdbDictValue> findByLabelOrValueAndDictCode(@NotNull String labelOrValue, @NotNull String dictCode);

    List<CmdbDictValue> findByDictCode(String dictCode);

    List<CmdbDictValue> findAllByValueIn(List<String> valueList);
}

